package com.cloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloud.entity.Score;
import com.cloud.service.ScoreService;
import com.cloud.service.impl.ScoreServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ScoreServlet", value = "/scoreServlet")
public class ScoreServlet extends BaseServlet {

    private ScoreService service = new ScoreServiceImpl();

    /**
     * 根据考试id查看考试成绩
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void findScoreById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String examId = request.getParameter("examId");
        int exam_id = Integer.parseInt(examId);

        List<Score> scores = service.findScoreById(exam_id);
        request.setAttribute("examId",exam_id);
        request.setAttribute("scores",scores);
        request.getRequestDispatcher("pages/score.jsp").forward(request,response);
    }

    /**
     * 根据用户id查看我的考试结果
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void findMyExamsRecord(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userId = request.getParameter("userId");
        List<Score> myexams = service.findMyExamsRecord(Integer.parseInt(userId));
        request.setAttribute("myexams",myexams);
        request.getRequestDispatcher("pages/myexam-list.jsp").forward(request,response);
    }
    /**
     * 查看是否已经考试
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void ifAttended(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userId = request.getParameter("userId");
        String examId = request.getParameter("examId");

        boolean res = service.ifAttended(userId, examId);
        JSONObject obj = new JSONObject();
        obj.put("result",res);
        response.getWriter().println(obj);
    }

}
